import emitter from "./emitter";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property(sp.Skeleton)
    spPlane: sp.Skeleton = null;
    @property(cc.Node)
    nodeZhuWang: cc.Node = null;
    @property(cc.Node)
    nodeCollider: cc.Node = null;
    _collider = null;
    @property(cc.Node)
    nodeInvincible: cc.Node = null;
    plane_blood = 3;

    onLoad() {
        this._collider = this.nodeCollider.getComponent("planeCollider");
        this._collider.setPlaneHarm = this.setPlaneState.bind(this);
        this.addEventListener();
    }

    addEventListener() {
        emitter.on("setBulletType", this.setBulletType.bind(this));
        emitter.on("setPlaneSpAnimation", this.setPlaneSpAnimation.bind(this));
    }

    onDisable() {
        emitter.off("setBulletType", this.setBulletType.bind(this));
        emitter.off("setPlaneSpAnimation", this.setPlaneSpAnimation.bind(this));
    }

    //根据当前显示的类型显示子弹类型
    setBulletType(node: cc.Node) {
        let type = node.getChildByName("goods").getComponent(cc.Sprite).spriteFrame.name;
        emitter.emit("changeBulletType", type);
    }

    //设置战机动画
    setPlaneSpAnimation(runState) {
        if (runState == 0) this.spPlane.setAnimation(1, "run", true);
        else this.spPlane.setAnimation(1, "run2", true);
    }

    //设置战机状态
    setPlaneState(state) {
        var self = this;
        switch (state) {
            case "zhuWang"://蜘蛛网
                emitter.emit("stopMove", false);
                this.nodeZhuWang.active = true;
                this.scheduleOnce(() => {
                    emitter.emit("stopMove", true);
                    self.nodeZhuWang.active = false;
                }, 1)
                break;
            case "invincible"://无敌
                break
            case 1://玩家受到的伤害 固定值 1
                emitter.emit("setBlood", 1);
                break
            default:
                break;
        }
    }
}
